package io.renren.modules.sys.service.impl;

import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.modules.sys.dao.SysBottomDao;
import io.renren.modules.sys.entity.SysBottomEntity;
import io.renren.modules.sys.service.SysBottomService;



@Service("sysBottomService")
public class SysBottomServiceImpl implements SysBottomService {

	@Autowired
	private SysBottomDao sysBottomDao;
	
	@Override
	public SysBottomEntity queryObject(Long bottomId){
		return sysBottomDao.queryObject(bottomId);
	}
	
	@Override
	public List<SysBottomEntity> queryList(Map<String, Object> map){
		return sysBottomDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return sysBottomDao.queryTotal(map);
	}
	
	@Override
	public void save(SysBottomEntity sysBottom){
		sysBottomDao.save(sysBottom);
	}
	
	@Override
	public void update(SysBottomEntity sysBottom){
		sysBottomDao.update(sysBottom);
	}
	
	@Override
	public void delete(Long bottomId){
		sysBottomDao.delete(bottomId);
	}
	
	@Override
	public void deleteBatch(Long[] bottomIds){
		sysBottomDao.deleteBatch(bottomIds);
	}

	@Override
	public SysBottomEntity queryCurrent() {

		Map<String, Object> param = Maps.newHashMap();
		param.put("offset", 0);
		param.put("limit", 1);

		List<SysBottomEntity> bottomEntities = sysBottomDao.queryList(param);

		if (CollectionUtils.isNotEmpty(bottomEntities)) {
			SysBottomEntity bottomEntity = bottomEntities.get(0);

			Calendar bottomEndTime = Calendar.getInstance();
			bottomEndTime.setTime(bottomEntity.getCreateTime());
			bottomEndTime.add(Calendar.MINUTE, bottomEntity.getDuration());
			//当前期次未结束
			if (bottomEndTime.getTime().after(new Date())) {
				return bottomEntity;
			}
		}
		return null;
	}

}
